package com.jucai.pppeiyouclient.base;

import android.app.Dialog;
import android.content.Context;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.jucai.pppeiyouclient.R;

/**
 * Created by Kuky on 2017-04-15.
 */

public abstract class BaseDialog extends Dialog {
    protected Context context;

    /**
     * 默认 dialog 风格
     */
    protected int baseDialogStyle = 0;

    /**
     * 默认 dialog 的宽度
     */
    protected double baseDialogWidth = 0;

    public BaseDialog(Context context) {
        super(context);
        init(context);
    }

    public BaseDialog(Context context, int themeResId) {
        super(context, themeResId);
        init(context);
    }

    protected BaseDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        init(context);
    }

    protected void init(Context context) {
        this.context = context;

        /**
         * 设置 dialog 的风格
         */
        baseDialogStyle = R.style.AlertDialogStyle;

        /**
         * 设置 dialog 的默认宽度
         */
        baseDialogWidth = context.getResources().getDisplayMetrics().widthPixels * 0.8;

        initView();
        setView(initView());
    }

    protected abstract View initView();

    private void setView(View view) {
        setContentView(view);
        setWidth();
    }

    private void setWidth() {
        Window dialogWindow = getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = (int) baseDialogWidth;
        getWindow().setAttributes(lp);

        setCanceledOnTouchOutside(true);
    }

    /**
     * 设置高度
     *
     * @param height
     */
    public void setHeight(int height) {
        Window dialogWindow = getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.height = height;
        getWindow().setAttributes(lp);
    }


    public void setInputMode() {
        Window dialogWindow = getWindow();
        dialogWindow.clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
        dialogWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE |
                WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
    }

    public abstract void setOnclickListener(View.OnClickListener listener);
}
